#ifndef DO_REAL_H
#define DO_REAL_H

/*
Copyright (c) 2013, ebaklund
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, 
   this list of conditions and the following disclaimer in the documentation 
   and/or other materials provided with the distribution.

3. Neither the name of the <ORGANIZATION> nor the names of its contributors 
   may be used to endorse or promote products derived from this software 
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/

#include <math.h>

namespace Do
{
  template<typename T> class Real
  {
  private:
    T val_;

  public:
    static size_t   size() { return sizeof(T); }

    static T abs( T a)  { return (a<0)?-a:a; }
    static T sign(T a)  { return (a< 0)?T(-1):(a>0)?T(1):0; }
    static T sqr(T a)   { return a*a; }
    static T inv(T a)   { return T(1)/a; }
    static T ceil(T a)  { return ::ceil(a); }
    static T floor(T a) { return ::floor(a); }
    static T round(T a) { return (a<0)?::ceil(a-0.5f): ::floor(a+0.5f); }
    static T trunc(T a) { return (a<0)?::ceil(a): ::floor(a); }
    static T truncn(T a, uint32_t n) { T p=::pow(T(10),T(n)); return trunc(a*p)/p; }

    static T max(T a, T b)  { return (a>=b)?a:b; }
    static T min(T a, T b)  { return (a<=b)?a:b; }
    static T amax(T a, T b) { return max(abs(a),abs(b)); }
    static T amin(T a, T b) { return min(abs(a),abs(b)); }
    static T dist(T a, T b) { return abs(a - b); }

    static T eps();
    static T epsb( uint8_t c)             { return (c==0)?0:eps()*(1<<(c-1)); }
    static T slack(T a)                   { return abs(a)*epsb(4); }
    static T slack(T a, T b)              { return amax(a,b)*epsb(4); }
    static T slackb(T a, uint32_t c)      { return abs(a)*epsb(c); }
    static T slackb(T a, T b, uint32_t c) { return amax(a,b)*epsb(c); }

    static bool eq(T a, T b)  { return dist(a,b)<=slack(a,b); }
    static bool neq(T a, T b) { return dist(a,b)>slack(a,b); }
    static bool lt(T a, T b)  { return (a-b)<-slack(a,b); }
    static bool lte(T a, T b) { return (a-b)<slack(a,b); }
    static bool gt(T a, T b)  { return (a-b)>slack(a,b); }
    static bool gte(T a, T b) { return (a-b)>-slack(a,b); }

    static bool eqb(T a, T b, uint32_t c)  { return dist(a,b)<=slackb(a,b,c); }
    static bool neqb(T a, T b, uint32_t c) { return dist(a,b)>slackb(a,b,c); }
    static bool ltb(T a, T b, uint32_t c)  { return (a-b)<-slackb(a,b,c); }
    static bool lteb(T a, T b, uint32_t c) { return (a-b)<slackb(a,b,c); }
    static bool gtb(T a, T b, uint32_t c)  { return (a-b)>slackb(a,b,c); }
    static bool gteb(T a, T b, uint32_t c) { return (a-b)>-slackb(a,b,c); }

    Real() {} // Uninitialized
    Real(Real<T> const& that) : val_(that.val_) { }
    Real(T const& val)          : val_(val)       { }

    Real<T>& operator=(Real<T> const& that) { val_ = that.val_; return *this; }
    Real<T>& operator=(T val)                 { val_ = val; return *this; }

    T val() { return val_; }

    Real<T> abs()    { return Real::abs(val_); }
    Real<T> sign()   { return Real::sign(val_); }
    Real<T> sqr()    { return Real::sqr(val_); }
    Real<T> inv()    { return Real::inv(val_); }
    Real<T> ceil()   { return Real::ceil(val_); }
    Real<T> floor()  { return Real::floor(val_); }
    Real<T> round()  { return Real::round(val_); }
    Real<T> trunc()  { return Real::trunc(val_); }
    Real<T> truncn(uint32_t n)  { return Real::truncn(val_, n); }

    Real<T> dist(Real<T> that) { return Real<T>::dist(val_, that.val_); }

    bool eq(Real<T> that)   { return Real<T>::eq(val_, that.val_); }
    bool neq(Real<T> that)  { return Real<T>::neq(val_, that.val_); }
    bool lt(Real<T> that)   { return Real<T>::lt(val_, that.val_); }
    bool lte(Real<T> that)  { return Real<T>::lte(val_, that.val_); }
    bool gt(Real<T> that)   { return Real<T>::gt(val_, that.val_); }
    bool gte(Real<T> that)  { return Real<T>::gte(val_, that.val_); }

    bool eqb(Real<T> that, uint8_t c)  { return Real<T>::eq(val_, that.val_, c); }
    bool neqb(Real<T> that, uint8_t c) { return Real<T>::neq(val_, that.val_, c); }
    bool ltb(Real<T> that, uint8_t c)  { return Real<T>::lt(val_, that.val_, c); }
    bool lteb(Real<T> that, uint8_t c) { return Real<T>::lte(val_, that.val_, c); }
    bool gtb(Real<T> that, uint8_t c)  { return Real<T>::gt(val_, that.val_, c); }
    bool gtbe(Real<T> that, uint8_t c) { return Real<T>::gte(val_, that.val_, c); }

  }; // Real


  template<> inline static float   Real<float>::eps()  { return FLT_EPSILON; }
  template<> inline static double  Real<double>::eps() { return DBL_EPSILON; }

} // DO

#endif // DO_REAL_H